﻿$(function () {
    $('.navigateToSearch').css('cursor', 'pointer').click(function () {
        $('<input />').attr('type', 'hidden')
.attr('name', 'action')
.val('navigateToSearch').appendTo($('form:eq(0)'));
        $('form:eq(0)').submit();
    });

    $('.dp input').datepicker({
        showOn: "button",
        buttonImage: '/Data/SiteImages/calendar.gif',
        buttonImageOnly: true,
        dateFormat: 'dd-mm-yy',
        minDate: new Date(),
        maxDate: new Date(parseInt($('select[name*=Year]:first option:last').val()), 11, 31),
        onSelect: function (dateText, inst) { onSelectCalendar(dateText, inst); },
        defaultDate: new Date(),
        beforeShow: function (input, inst) { beforeShowCalendar(input, inst); }
    });

    $('.dp1 select').change(incrementDepartureByArrival);

    $('select[name*=Month]').change(function () {
        var md = parseInt($(this).find('option[value=' + $(this).val() + ']').attr('m-d'));
        var sDay = $(this).parent().parent().find('select[name*=Day]');
        if (parseInt(sDay.val()) > md)
            sDay.val(md);
        sDay.find('option').each(function () {
            if (parseInt($(this).attr('value')) > md)
                $(this).attr('disabled', 'disabled');
            else
                $(this).removeAttr('disabled');
        });
    });

    $('.dp1').parent().parent().find('input,select').change(function () {
        showWarningFormChange();
    });

    var width = $(window).width() * 0.95;
    if (width < 1000)
        width = 1000;
    width = Math.ceil(width / $(window).width() * 100) + '%';

    $('.rsi-d').fancybox({
        'width': width,
        'height': '95%',
        'autoScale': false,
        'transitionIn': 'fadeIn',
        'transitionOut': 'none',
        'type': 'iframe'
    });
});

function beforeShowCalendar(input, inst) {
    var r = $(input).parent().parent();
    var ddlDay = parseInt(r.find('select[id*=Day]').val());
    var ddlMonth = parseInt(r.find('select[id*=Month]').val()) - 1;
    var ddlYear = parseInt(r.find('select[id*=Year]').val());


    $(input).datepicker('setDate', new Date(ddlYear, ddlMonth, ddlDay));
}

function onSelectCalendar(dateText, inst) {
    var r = $(inst.input).parent().parent();
    r.find('select[id*=Day]').val(inst.currentDay);
    r.find('select[id*=Month]').val((inst.currentMonth + 1));
    r.find('select[id*=Year]').val((inst.currentYear));
    if ($(inst.input).parent().parent().is('.dp1')) {
        incrementDepartureByArrival();
    }
    showWarningFormChange();
}

function incrementDepartureByArrival() {
    var day = parseInt($('.dp1 select[name*=Day]').val());
    var month = parseInt($('.dp1 select[name*=Month]').val()) - 1;
    var year = parseInt($('.dp1 select[name*=Year]').val());

    var arDate = new Date(year, month, day, 12, 0, 0, 0);
    arDate.setDate(arDate.getDate() + 1);
    $('.dp2 select[name*=Day]').val(arDate.getDate());
    $('.dp2 select[name*=Month]').val(arDate.getMonth() + 1);
    $('.dp2 select[name*=Year]').val(arDate.getFullYear());
}

function showWarningFormChange() {
    if (!$('.rsi-formchange').is('*:visible')) {
        $('.rsi-formchange').css({
            position: 'absolute',
            visibility: 'hidden'
        });
        var h = $('.rsi-formchange').height();
        $('.rsi-formchange').height(1);
        $('.rsi-formchange').css({
            position: '',
            visibility: ''
        });
        $('.rsi-formchange').show().animate({ height: h + 'px' });
    }
}